package br.com.wuo.genericInventary.application;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.swing.Timer;

import br.com.wuo.genericInventary.model.Item;
import br.com.wuo.genericInventary.view.GenericInventory;

import com.thingmagic.ReadListener;
import com.thingmagic.Reader;
import com.thingmagic.TagReadData;

/**
 *
 * @author renan wuo
 */
public class GenericInventary implements ReadListener { 
    private ShelfReader reader;
    private boolean firstUpdate;
    private Timer timer;
    private Properties properties;
    private Collection<String> currentEpcs;
    private Collection<String> previousEpcs;
    private GenericInventory genericInventory;
    private Map<String, String> itemName;
    private Map<String, Integer> itemQuantity;
    private ArrayList<Item> items;

    public GenericInventary() {
        
    	try {
			Resources.verifyResources();
			properties = Resources.getProperties();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.exit(1);
		}
    	
    	this.firstUpdate = true;
    	
        genericInventory = new GenericInventory();
        genericInventory.setVisible(true);
        
        createAndLoadItemsMap();
        
        
        try {
        	reader = new ShelfReader("");
			reader.connect();
			shelfCount();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }

	private void shelfCount(){
    	
    	timer = new Timer(300000, new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					reader.startReading();
					Thread.sleep(15000);
					reader.stopReading();
					
					/*TagReadData[] tags = reader.read(15000);
					
					int size = tags.length;
					
					for(int i=0; i == size; i++){
						if(!currentEpcs.contains(tags[i].epcString())){
							currentEpcs.add(tags[i].epcString());
						}
					}*/
					
					if(firstUpdate){
			    		firstUpdate = false;
			    		previousEpcs = currentEpcs;
			    		currentEpcs.clear();
			    	}else{
			    		if(previousEpcs.containsAll(currentEpcs) && currentEpcs.containsAll(previousEpcs)){
			    			currentEpcs.clear();

			    		}else{
			    			previousEpcs = currentEpcs;
			    			currentEpcs.clear();
			    		}
			    	}
					
				} catch (IOException e1) {
					e1.printStackTrace();
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				} /*catch (ReaderException e1) {
					e1.printStackTrace();
				}*/
				
				calculateItemsRead();
				
				genericInventory.updateTable(items);
				
			}
		});
    	
		timer.start();
    }
    
    public void calculateItemsRead(){
    	
    	for(String epc : currentEpcs){
    		
    		char[] ecpUnstrig = epc.toCharArray();
    		char[] upcChar = new char[8];
    		upcChar[0] = ecpUnstrig[0];
    		upcChar[1] = ecpUnstrig[1];
    		upcChar[2] = ecpUnstrig[2];
    		upcChar[3] = ecpUnstrig[3];
    		upcChar[4] = ecpUnstrig[4];
    		upcChar[5] = ecpUnstrig[5];
    		upcChar[6] = ecpUnstrig[6];
    		upcChar[7] = ecpUnstrig[7];
    		
    		String upc = upcChar.toString();
    		
    		if(itemName.containsKey(upc)){
    			int iq = itemQuantity.get(upc);
    			itemQuantity.put(upc, iq + 1);
    		}
    	}
    	
    	Set<String> keys = itemQuantity.keySet();
    	
    	for(String key : keys){
    		String name = itemName.get(key);
    		int quantity = itemQuantity.get(key);
    		
    		Item item = new Item(name, quantity);
    		items.add(item);
    	}
    	
    }
    
    private void createAndLoadItemsMap() {
		
		itemName = new HashMap<String, String>();
		itemName.put("00001234", "Camisa Branca P");
		itemName.put("00001235", "Camisa Branca M");
		itemName.put("00001236", "Camisa Branca G");
		
		itemQuantity = new HashMap<String, Integer>();
		itemQuantity.put("00001234", 0);
		itemQuantity.put("00001235", 0);
		itemQuantity.put("00001236", 0);
	}
    

	@Override
	public void tagRead(Reader reader, TagReadData t) {
		if(!currentEpcs.contains(t.epcString())){
			currentEpcs.add(t.epcString());
		}
	}

}
